#include "page2.h"
#include "ui_page2.h"
#include<QPixmap>
#include<QFrame>
#include<QHBoxLayout>
#include<QFileDialog>
#include<QString>


Page2::Page2(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Page2)
{
    ui->setupUi(this);
    mediaPlayer = new QMediaPlayer(this);
    //设置mediaPlayer的QVideoWidget播放窗口
    mediaPlayer->setVideoOutput(ui->widgetMed);

    //设置播放属性
    //1 监听信号变化函数
    connect(mediaPlayer,SIGNAL(stateChanged(QMediaPlayer::State)),this,SLOT(mediaStateChanged(QMediaPlayer::State)));
    //2 播放进度信号变化函数
    connect(mediaPlayer,SIGNAL(positionChanged(qint64)),this,SLOT(positionChanged(qint64)));
    //3 播放长度信号变化
    connect(mediaPlayer,SIGNAL(durationChanged(qint64)),this,SLOT(durationChanged(qint64)));
    //4 设置播放错误关联
    connect(mediaPlayer,SIGNAL(error(QMediaPlayer::Error)),this,SLOT(handleError()));

    //设置播放按钮
    ui->toolButton->setToolTip(QString::fromLocal8Bit("播放"));//设置鼠标点上去提示
    ui->toolButton->setAutoRaise(true);
    ui->toolButton->setIcon(QPixmap(":/image/play.png"));
    ui->toolButton->setIconSize(QSize(500,500));

    //设置打开文件按钮
    ui->toolButton_2->setToolTip(QString::fromLocal8Bit("打开文件"));
    ui->toolButton_2->setAutoRaise(true);//设置自动获取状态
    ui->toolButton_2->setIcon(QPixmap(":/image/openfile.png"));
    ui->toolButton_2->setIconSize(QSize(75,75));

    ui->horizontalSlider->setStyleSheet("  \
         QSlider::add-page:Horizontal\
         {     \
            background-color: rgb(87, 97, 106);\
            height:14px;\
         }\
         QSlider::sub-page:Horizontal \
        {\
            background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(231,80,229, 255), stop:1 rgba(7,208,255, 255));\
            height:14px;\
         }\
        QSlider::groove:Horizontal \
        {\
            background:transparent;\
            height:14px;\
        }\
        QSlider::handle:Horizontal \
        {\
            height: 30px;\
            width:8px;\
            border-image: url(:/images/ic_music_thumb.png);\
            margin: -8 0px; \
        }\
        ");
}

Page2::~Page2()
{
    delete ui;
}

void Page2::on_toolButton_clicked()
{
    switch (mediaPlayer->state()) {
    case QMediaPlayer::PlayingState:
        mediaPlayer->pause();
        ui->toolButton->setIcon(QPixmap(":/image/play.png"));
        break;
    default:
        mediaPlayer->play();
        ui->toolButton->setIcon(QPixmap(":/image/pause.jpg"));
        break;
    }
    qint64 playtime = mediaPlayer->duration();
    qDebug()<<playtime;
    emit sendVideoTime(playtime);
}
void Page2::mediaStateChanged(QMediaPlayer::State state)
{
    //mediaPlayer播放过程中动态调整播放进度
    switch (state) {
    case QMediaPlayer::PlayingState:
        ui->toolButton->setToolTip(QString::fromLocal8Bit("暂停"));
        //ui->toolButton->setIcon(QPixmap(":/image/pause.jpg"));
        emit sendNewStr("[消息]视频正在播放...");    
        break;
    default:
        ui->toolButton->setToolTip(QString::fromLocal8Bit("播放"));
        //ui->toolButton->setIcon(QPixmap(":/image/play.png"));
        emit sendNewStr("[消息]视频已暂停...");
        break;
    }

}
void Page2::positionChanged(qint64 position)
{
    //改变播放位置
    ui->horizontalSlider->setValue(position);

}
void Page2::setPosition(int position)
{
    //获取mediaPlayer进度条调整位置
    mediaPlayer->setPosition(position);

}
void Page2::durationChanged(qint64 duration)
{
    //获取进度条的范围
    ui->horizontalSlider->setRange(0,duration);

}

void Page2::on_horizontalSlider_sliderMoved(int position)
{
    setPosition(position);
}
void Page2::handleError()
{
    ui->toolButton->setEnabled(false);
}

void Page2::on_toolButton_2_clicked()
{
    filename = QFileDialog::getOpenFileName(this,QString::fromLocal8Bit("打开视频"),"G:/",tr("*.wmv *.avi"));
    //设置播放内容
    mediaPlayer->setMedia(QUrl::fromLocalFile(filename));
    ui->toolButton->setAutoRaise(true);
    emit sendNewStr("[消息]"+filename+"已经读入系统...");
    emit sendVideoPath(filename);

}
